﻿$(document).ready(function () {
    ko.bindingHandlers.markdown = {
        init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
        },
        update: function (element, valueAccessor, allBindingsAccessor, viewModel) {
            var value = ko.utils.unwrapObservable(valueAccessor());
            var html = new Showdown.converter().makeHtml(value);

            $(element).html(html);
        }
    };

    function Article() {
        var self = this;

        this.filename = ko.observable('');
        this.content = ko.observable('');

        this.init = function () {
            self.filename('');
            self.content('');
        };
        if (Services.localSupport) {
            this.open = function () {
                Services.local({
                    method: 'open',
                    params: self.filename(),
                    complete: function (data) {
                        if (data) {
                            self.filename(data.filename);
                            self.content(data.content);
                        }
                    }
                });
            };
            this.save = function () {
                Services.local({
                    method: 'save',
                    params: {
                        filename: self.filename(),
                        content: self.content()
                    },
                    complete: function (data) {
                        if (data) {
                            self.filename(data);
                        }
                    }
                });
            };
        }
        else {
            this.open = function () {
            };
            this.save = function () {
            };
        }
    }

    ko.applyBindings(new Article());
});

$(window).load(function () {
    $(window).trigger('resize');
});